﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OreSystem.Common;
namespace MainFrom
{
    public partial class FormAttach_db : Form
    {
        public FormAttach_db()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string dbFile = textBox1.Text;
            string dbLogFile = textBox2.Text;
            if (string.IsNullOrEmpty(dbFile) || string.IsNullOrEmpty(dbLogFile))
            {
                MessageBox.Show("请选择数据文件!");
                return;
            }
            string addSql = string.Format(@"
use master
if Exists(select * from dbo.sysdatabases where name='OreSystemData')
begin
--drop database 'OreSystemData'
EXEC sp_detach_db 'OreSystemData', 'true';
end
else
begin
EXEC sp_attach_db @dbname = 'OreSystemData', 
@filename1 = '{0}', 
@filename2 = '{1}'
end", dbFile, dbLogFile);
            try
            {
                MsSqlHelper.ExceuteNoQuery(addSql, null);
                MessageBox.Show("附加成功");
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = @"D:\DataBase";
            ofd.Filter = "*.mdf|*.mdf";
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                textBox1.Text = ofd.FileName;
                textBox2.Text = textBox1.Text.Substring(0, textBox1.Text.IndexOf(".")) + "_log.ldf";
            }
        }
    }
}
